<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">java中equals和==的区别</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-09-16</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h3 id="java中equals和-的区别"><a href="#java中equals和-的区别" class="headerlink" title="java中equals和==的区别"></a>java中equals和==的区别</h3><p>对于基本类型来说，==是对数值的比较。</p>
<p>对于引用类型来说，==是对地址值的比较。</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int <span class="attribute">num1</span>=1;</span><br><span class="line">int <span class="attribute">num2</span>=1;</span><br><span class="line">System.out.println(<span class="attribute">num1</span>==num2)//true</span><br></pre></td></tr></table></figure>

<p>因为int是基本类型，基本类型中==是对数值比较。</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">String</span> str1=<span class="string">"abc"</span>;</span><br><span class="line"><span class="keyword">String</span> str2=<span class="string">"abc"</span>;</span><br><span class="line"><span class="keyword">char</span>[] charsArray=&#123;</span><br><span class="line">	<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">String</span> str3=<span class="keyword">new</span> <span class="keyword">String</span>(charsArray);</span><br><span class="line"></span><br><span class="line">System.out.<span class="built_in">println</span>(str1==str2);<span class="comment">//true</span></span><br><span class="line">System.out.<span class="built_in">println</span>(str1==str3);<span class="comment">//false</span></span><br><span class="line">System.out.<span class="built_in">println</span>(str2==str3);<span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">System.out.<span class="built_in">println</span>(str1.equals(str2));<span class="comment">//true</span></span><br><span class="line">System.out.<span class="built_in">println</span>(str1.equals(str3));<span class="comment">//true</span></span><br><span class="line">System.out.<span class="built_in">println</span>(str2.equals(str3));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>原文链接：<a href="https://blog.csdn.net/weixin_40304387/article/details/81071816" target="_blank" rel="noopener">https://blog.csdn.net/weixin_40304387/article/details/81071816</a> （以下文字看原文链接理解）</p>
</blockquote>
<blockquote>
<p>采用字面值的方式创建一个字符串时，JVM（Java Virtual Machine，即java虚拟机）首先会去字符串池中查找是否存在”abc”这个对象，如果不存在，则在字符串常量池中创建”abc”这个对象，然后将池中”abc”这个对象的引用地址返回给”abc”对象的引用s1，这样s1会指向字符串常量池中”abc”这个字符串对象；如果存在，则不创建任何对象，直接将池中”abc”这个对象的地址返回，赋给引用s2。因为s1、s2都是指向同一个字符串池中的”abc”对象，所以结果为true。</p>
<p>采用new关键字新建一个字符串对象时，JVM首先在字符串池中查找有没有”abc”这个字符串对象，如果有，则不在池中再去创建”abc”这个对象了，直接在堆中创建一个”abc”字符串对象，然后将堆中的这个”abc”对象的地址返回赋给引用s3，这样，s3就指向了堆中创建的这个”abc”字符串对象；如果没有，则首先在字符串池中创建一个”abc”字符串对象，然后再在堆中创建一个”abc”字符串对象，然后将堆中这个”abc”字符串对象的地址返回赋给s3引用，这样，s3指向了堆中创建的这个”abc”字符串对象。s4则指向了堆中创建的另一个”abc”字符串对象。s3 、s4是两个指向不同对象的引用，结果当然是false。</p>
</blockquote>
<img src="/2019/09/16/equals/字符串的常量池.png" title="字符串常量池">

<p>在堆内存中（从jdk1.7开始），有一个空间，叫字符串常量池。字符串直接创建的，在字符串常量池中存储。而在字符串常量池中，存储着字符串对象。字符串在最底层是用byte数组存储的。</p>
<p>new关键字创建时，会把char型数组转换成字符数组，然后创建。</p>
<h3 id="基本类型和引用类型"><a href="#基本类型和引用类型" class="headerlink" title="基本类型和引用类型"></a>基本类型和引用类型</h3><ul>
<li><p>基本类型</p>
<ul>
<li>整型<ul>
<li>byte</li>
<li>short</li>
<li>int</li>
<li>long</li>
</ul>
</li>
<li>浮点型<ul>
<li>float</li>
<li>double</li>
</ul>
</li>
<li>字符型<ul>
<li>char</li>
</ul>
</li>
<li>布尔型<ul>
<li>boolean</li>
</ul>
</li>
</ul>
</li>
<li><p>引用类型（java为每种基本类型，都提供了对应的封装类型）</p>
<ul>
<li>Byte</li>
<li>Short</li>
<li>Integer</li>
<li>Long</li>
<li>Float</li>
<li>Double</li>
<li>Character</li>
<li>Boolean</li>
</ul>
</li>
</ul>
<p>注意：在泛型类中，类型只能是引用类型，不能是基本类型。</p>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>